﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ManageTicket.Human
{
    public partial class frmUsersGroup : frmBase
    {
        #region Contructor
        public frmUsersGroup()
        {
            InitializeComponent();
        }
        #endregion

        #region Variable
        ManageTicket.Library.Control.ExecuteControl exeCtrl;

        //permission
        bool bAdd = false;
        bool bEdit = false;
        bool bDelete = false;
        bool bCancel = false;
        bool bSave = false;
        bool bFunction = false;
        bool bView = false;
        bool bFind = false;
        bool bExport = false;
        bool bSendmail = false;
        bool bPrint = false;

        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();
        private Business.UsersGroup boUsersGroup = new Business.UsersGroup();
        string sModel = ""; //Store AND or UPDATE status
        private const string ADD_MODEL = "ADD";
        private const string UPDATE_MODEL = "UPDATE";
        #endregion

        #region Event
        private void frmUsersGroup_Load(object sender, EventArgs e)
        {
            //execute control
            exeCtrl = new ManageTicket.Library.Control.ExecuteControl();
            exeCtrl.Dock = DockStyle.Fill;
            exeCtrl.lblDescription.Text = "Cập nhật nhóm người sử dụng";
            pnlExe.Controls.Add(exeCtrl);

            //permission
            clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_15");

            //set status
            LoadStatusOnce();

            View();
        }

        private void lvwUsersGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            sModel = "";
            IsEdit();
            if (lvwUsersGroup.SelectedItems.Count > 0)
            {
                ListViewItem item = lvwUsersGroup.SelectedItems[0];

                txtGroupId.Text = item.Text;
                txtGroupName.Text = item.SubItems[1].Text;
                txtRemark.Text = item.SubItems[2].Text;

                LoadStatus(true, true, true, false, false, true);
            }
            else
            {
                LoadStatus(true, false, false, false, false, true);
            }
        }
        #endregion

        #region Execute
        public override void Add()
        {
            objMethod.ClearText(new Control[] { txtGroupId, txtGroupName, txtRemark });
            LoadStatus(false, false, false, true, true, false);
            lvwUsersGroup.Enabled = false;
            sModel = ADD_MODEL;
            IsEdit();
            txtGroupId.Focus();
        }

        public override void Edit()
        {
            if (txtGroupId.Text == "")
                return;
            LoadStatus(false, false, false, true, true, false);
            lvwUsersGroup.Enabled = false;
            sModel = UPDATE_MODEL;
            IsEdit();
            txtGroupName.Focus();
        }

        public override void Delete()
        {
            try
            {
                if (txtGroupId.Text == "")
                    return;

                if (MessageBox.Show("Bạn có thực sự muốn xóa?", "Aquarius", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Check Existing in table
                    //if (this.CheckExitToDelete(oDept.DEPT_CODE)) {
                    //    MessageBox.Show(".", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    return;
                    //}

                    //delete
                    boUsersGroup.DeleteByPK(txtGroupId.Text);

                    MessageBox.Show("Xóa nhóm thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    View();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void Cancel()
        {
            objMethod.ClearText(new Control[] { txtGroupId, txtGroupName, txtRemark });
            LoadStatus(false, false, false, false, false, true);
            View();
        }

        public override void Save()
        {
            if (CheckValid())
            {
                try
                {
                    DataObjects.UsersGroup oGroup = new DataObjects.UsersGroup();
                    oGroup.GroupId = txtGroupId.Text;
                    oGroup.GroupName = txtGroupName.Text;
                    oGroup.Remark = txtRemark.Text;

                    if (sModel.Trim().ToUpper().Equals(ADD_MODEL))
                    {
                        if (CheckIsExist(txtGroupId.Text))
                        {
                            MessageBox.Show("Mã nhóm đã tồn tại.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtGroupId.Focus();
                            return;
                        }
                        //Insert
                        boUsersGroup.Insert(oGroup);
                    }
                    else if (sModel.Trim().ToUpper().Equals(UPDATE_MODEL))
                    {
                        boUsersGroup.Update(oGroup);
                    }
                    MessageBox.Show("Cập nhật nhóm thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    View();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + "Không thể cập nhật nhóm.", "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public override void View()
        {
            try
            {
                lvwUsersGroup.Items.Clear();

                ArrayList arrGroup = new ArrayList();
                arrGroup = boUsersGroup.List();

                ListViewItem item;

                foreach (DataObjects.UsersGroup oGroup in arrGroup)
                {
                    item = new ListViewItem();
                    item.Text = oGroup.GroupId;
                    item.SubItems.Add(oGroup.GroupName);
                    item.SubItems.Add(oGroup.Remark);

                    lvwUsersGroup.Items.Add(item);
                }

                if (lvwUsersGroup.Items.Count > 0)
                {
                    lvwUsersGroup.Items[0].Selected = true;
                    lvwUsersGroup.Enabled = true;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Method
        private void LoadStatusOnce()
        {
            exeCtrl.btnFunction.Enabled = false;
            exeCtrl.btnFind.Enabled = false;
            exeCtrl.btnExport.Enabled = false;
            exeCtrl.btnSendmail.Enabled = false;
            exeCtrl.btnPrint.Enabled = false;

            exeCtrl.btnFunction.BackColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFind.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnExport.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnSendmail.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnPrint.ButtonColor = Color.FromArgb(171, 171, 171);

            exeCtrl.btnFunction.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFind.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnExport.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnSendmail.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnPrint.ForeColor = Color.FromArgb(241, 244, 251);
        }

        private void LoadStatus(bool blAdd, bool blEdit, bool blDelete, bool blCancel, bool blSave, bool blView)
        {
            exeCtrl.btnAdd.Enabled = blAdd & bAdd;
            exeCtrl.btnEdit.Enabled = blEdit & bEdit;
            exeCtrl.btnDelete.Enabled = blDelete & bDelete;
            exeCtrl.btnCancel.Enabled = blCancel & bCancel;
            exeCtrl.btnSave.Enabled = blSave & bSave;
            exeCtrl.btnView.Enabled = blView & bView;

            exeCtrl.btnAdd.ButtonColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnEdit.ButtonColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnDelete.ButtonColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnCancel.ButtonColor = exeCtrl.btnCancel.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnSave.ButtonColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnView.ButtonColor = exeCtrl.btnView.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);

            exeCtrl.btnAdd.ForeColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnEdit.ForeColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnDelete.ForeColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnCancel.ForeColor = exeCtrl.btnCancel.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnSave.ForeColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnView.ForeColor = exeCtrl.btnView.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
        }

        private void IsEdit()
        {
            if (sModel == ADD_MODEL)
            {
                txtGroupId.ReadOnly = false;
                txtGroupName.ReadOnly = false;
                txtRemark.ReadOnly = false;
            }
            else if (sModel == UPDATE_MODEL)
            {
                txtGroupId.ReadOnly = true;
                txtGroupName.ReadOnly = false;
                txtRemark.ReadOnly = false;
            }
            else
            {
                txtGroupId.ReadOnly = true;
                txtGroupName.ReadOnly = true;
                txtRemark.ReadOnly = true;
            }
        }

        private bool CheckValid()
        {
            if (txtGroupId.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập mã nhóm.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtGroupId.Focus();
                return false;
            }
            else if (!objMethod.CheckSpecialCharacter(txtGroupId.Text))
            {
                MessageBox.Show("Mã nhóm không được nhập các ký tự đặc biệt.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtGroupId.Focus();
                return false;
            }

            if (txtGroupName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập tên nhóm.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtGroupName.Focus();
                return false;
            }
            return true;
        }

        private bool CheckIsExist(string id)
        {
            DataObjects.UsersGroup oGroup = boUsersGroup.GetObjectByPK(id);
            if (oGroup != null)
            {
                return true;
            }
            return false;
        }
        #endregion
    }
}
